package bingo.component.amchart;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.bingosoft.common.ObjectRegistry;
import net.bingosoft.common.struts.ActionContext;
import net.bingosoft.common.util.XMLUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Node;

import bingo.component.amchart.graphics.Graphic.GraphicType;
import bingo.component.amchart.service.GraphicReaderService;
import bingo.component.amchart.service.IGraphicReaderService;
/**
 * Jun 24, 2008 3:35:31 PM
 * Author:yangdongyu(yangdy@bingosoft.net)
 * ReadMe:AmChart数据加载的Servlet
 * ChangeLog:
 */
public class AmChartDataLoaderServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private static final Log log = LogFactory.getLog(AmChartDataLoaderServlet.class);

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException {
		//服务类的beanId
		String service = req.getParameter("service");
		String type = req.getParameter("chartType");
		String sqlId = req.getParameter("sqlKey");
		IGraphicReaderService serviceBean = null;
		if(service == null || !ObjectRegistry.containsBean(service)){
			//没有定义服务类,使用默认的服务类
			serviceBean = new GraphicReaderService();
		}else{
			serviceBean = ObjectRegistry.getBean(service,IGraphicReaderService.class);
		}
		Map <String,Object> paramsMap = getParameterMap(req);
		try {
			ActionContext.init(req, resp);
			Node graphicElement = serviceBean.getGraphic(sqlId,GraphicType.valueOf(type.trim().toLowerCase()),paramsMap).toXML();
			if(log.isDebugEnabled()){
				XMLUtil.dump(graphicElement, "GBK");
			}
			renderXML(resp, graphicElement.asXML());
		}  catch (Exception e) {
			e.printStackTrace();
		}finally{
			ActionContext.destory();
		}
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException {
		doPost(req, resp);
	}
	/**
     * 从request中获取请求参数，并以Map的形式返回
     * @param request
     * @return
     */
	@SuppressWarnings("unchecked")
	public Map<String,Object> getParameterMap(HttpServletRequest request) {
		Enumeration<String> names = request.getParameterNames();
		Map<String,Object> map = new HashMap<String,Object>();
		while (names.hasMoreElements()) {
			String name = (String) names.nextElement();
			String[] s = request.getParameterValues(name);
			if (s != null) {
				if (s.length > 1) {
					map.put(name, s);
				}else{
					map.put(name, s[0]);
				}
			}
		}
		return map;
	}
	/**
	 * 直接输出纯XML.
	 */
	public static void renderXML(HttpServletResponse response,String xml) {
		try {
			response.setContentType("text/xml;charset=UTF-8");
			response.getWriter().write(xml);
		} catch (IOException e) {
			log.error(e.getMessage(), e);
		}
	}

}
